require(['./config'], () => {
    require(['template', 'request', 'header', 'footer'], (template, request) => {
        class Statement {
            constructor() {
                this.init()

                this.buy()
            }

            init() {
                const _this = this
                let cartList = localStorage.getItem('cart')

                if (cartList) {
                    this.cartList = JSON.parse(cartList)
                    this.cartList = this.cartList.filter(cart => {
                        return cart.check == true
                    })
                    console.log(this.cartList)
                    $('tbody').html(template('listtemp', { cartList: this.cartList }))
                    this.totalmoney()
                } 
 
                this.Province()

                $('.province').click(function () {
                    _this.City()
                })

                $('.city').click(function () {
                    _this.Area()
                })

                this.ChoiceTime()

            }


            //计算总价
            totalmoney() {
                $('.totmoney').html(this.cartList.reduce((money, cart) => {
                    money += cart.price * cart.num
                    return money
                }, 0).toFixed(2))
            }

            // 省
            Province() {
                request.getProvince().then(resp => {
                    // console.log(resp)
                    var json = resp['result'][0]
                    var provinceHtml = "<option value=''>--请选择--</option>"
                    $.each(json, function (index, obj) {
                        provinceHtml += "<option value=" + obj.fullname + " province-id=" + obj.id + ">" + obj.fullname + "</option>";
                    })
                    $('.province').html(provinceHtml)
                })
            }

            // 市
            City() {
                var id = $('.province').find("option:selected").attr('province-id');
                request.getCity(id).then(resp => {
                    // console.log(resp)
                    var json = resp['result'][0]
                    var provinceHtml
                    $.each(json, function (index, obj) {
                        provinceHtml += "<option value=" + obj.fullname + " city-id=" + obj.id + ">" + obj.fullname + "</option>";
                    })
                    $('.city').html(provinceHtml)
                })
            }
            // 区
            Area() {
                var id = $('.city').find("option:selected").attr('city-id');
                request.getArea(id).then(resp => {
                    // console.log(resp)
                    var json = resp['result'][0]
                    var provinceHtml
                    $.each(json, function (index, obj) {
                        provinceHtml += "<option value=" + obj.fullname + ">" + obj.fullname + "</option>";
                    })
                    $('.area').html(provinceHtml)
                })
            }

            // 选择配送时间
            ChoiceTime() {
                $('.ct li a').click(function () {
                    $(this).parent('li').each(function() {
                        $(this).toggleClass("choicecheck").siblings().removeClass('choicecheck')
                    })
                })

            }

            buy() {
                const _this = this
                $('.subm').click(function () {
                    let cartList = localStorage.getItem('cart')
                    cartList = JSON.parse(cartList)
                    console.log(cartList)
                    if (cartList.length !=0) {
                        alert('支付成功!')
                        
                    } else {
                        alert('请去选购')
                    }
                })
            }



        }
        new Statement()
    })
})